﻿using System.Linq.Expressions;
using NRules.RuleModel.Builders;

namespace NRules.RuleModel;

/// <summary>
/// Extension methods to convert between <see cref="Declaration"/> and <see cref="ParameterExpression"/>.
/// </summary>
public static class DeclarationExtensions
{
    /// <summary>
    /// Converts rule element <see cref="Declaration"/> to a <see cref="ParameterExpression"/>.
    /// </summary>
    /// <param name="declaration">Declaration to convert.</param>
    /// <returns>Parameter expression.</returns>
    public static ParameterExpression ToParameterExpression(this Declaration declaration)
    {
        return Expression.Parameter(declaration.Type, declaration.Name);
    }

    /// <summary>
    /// Converts <see cref="ParameterExpression"/> to a rule element <see cref="Declaration"/>.
    /// </summary>
    /// <param name="parameter">Parameter expression to convert</param>
    /// <returns>Rule element declaration.</returns>
    public static Declaration ToDeclaration(this ParameterExpression parameter)
    {
        return Element.Declaration(parameter.Type, parameter.Name);
    }
}